package cn.elead.chaos.sso.core.util;

import cn.elead.chaos.sso.core.user.LoginUser;

public class SsoUtils {

	public static final String LOGIN_USER_KEY = Context.class.getName() + "_LOGIN_USER_KEY";

	public static LoginUser getUser() {
		return (LoginUser) Context.get(LOGIN_USER_KEY);
	}

	public static String getUserId() {
		LoginUser user = getUser();
		if (null != user) {
			return user.getUserId();
		}
		return null;
	}
	
	public static String getUserName() {
		LoginUser user = getUser();
		if (null != user) {
			return user.getUserName();
		}
		return null;
	}
	
	public static String getToken() {
		LoginUser user = getUser();
		if (null != user) {
			return user.getToken();
		}
		return null;
	}
	
	public static String getTenant() {
		LoginUser user = getUser();
		if (null != user) {
			return user.getTenant();
		}
		return null;
	}

	public static boolean isAdmin() {
		return isAdmin(getUserId());
	}
	
	public static boolean isAdmin(String userId) {
		if ("admin".equals(userId)) {
			return true;
		}
		return false;
	}
	
	public static void bind(LoginUser user) {
		if (user != null) {
			Context.put(LOGIN_USER_KEY, user);
		}
	}

	public static LoginUser unbindUser() {
		return (LoginUser) Context.remove(LOGIN_USER_KEY);
	}
}
